package org.elephas.service;

import java.util.HashSet;
import java.util.Set;

import org.elephas.model.Blog;
import org.elephas.model.BlogConfiguration;
import org.elephas.model.BlogMembership;
import org.elephas.model.BlogRole;
import org.elephas.model.Category;
import org.elephas.model.CommentStatus;
import org.elephas.model.Post;
import org.elephas.model.PostStatus;
import org.elephas.model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.InitializingBean;

public class InsertTestDataService implements InitializingBean {

	private SessionFactory sessionFactory;

	private Session session;

	public void afterPropertiesSet() throws Exception {
		session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Blog blog = blog();
		User user = user();
		blogmembership(blog, user, BlogRole.BLOG_ADMIN);
		Category category = category(blog);
		post("first post", user, category, blog);
		post("second post", user, category, blog);
		tx.commit();
		session.close();
	}

	private BlogMembership blogmembership(Blog blog, User user, BlogRole role) {
		BlogMembership ms = new BlogMembership();
		ms.setBlog(blog);
		ms.setUser(user);
		ms.setBlogRole(role);
		session.save(ms);
		return null;
	}

	private Post post(String title, User user, Category category, Blog blog) {
		Post post = new Post();
		post.setBlog(blog);
		Set<Category> categories = new HashSet<Category>();
		post.setCategories(categories);
		post.getCategories().add(category);
		post.setCommentStatus(CommentStatus.OPEN);
		post
				.setContent("Lorem ipsum dolor sit amet, consectetuer sadipscing elitr, "
						+ "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, "
						+ "sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. "
						+ "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");
		post.setTitle(title);
		post
				.setExcerpt("Lorem ipsum dolor sit amet, consectetuer sadipscing elitr, "
						+ "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, "
						+ "sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. "
						+ "Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");
		post.setPostStatus(PostStatus.PUBLISHED);
		post.setUser(user);
		session.save(post);
		return post;
	}

	private Category category(Blog blog) {
		Category cat = new Category();
		cat.setName("elephas");
		cat.setInternalName("cat:elephas");
		cat.setDescription("everything related to elephas");
		cat.setBlog(blog);
		session.save(cat);
		return cat;
	}

	private Blog blog() {
		BlogConfiguration config = new BlogConfiguration();
		config.setTheme("blue");
		Blog blog = new Blog();
		blog.setName("elephantastic");
		blog.setDescription("check out the elephant");
		blog.setConfiguration(config);
		config.setBlog(blog);
		session.save(config);
		session.save(blog);
		return blog;
	}

	private User user() {
		User user = new User();
		user.setEmailAddress("admin@elephas.org");
		user.setFirstName("Joe");
		user.setLastName("Merrick");
		user.setOrganization("elephas.org");
		user.setPassword("passme");
		session.save(user);
		return user;
	}

	public void setSessionFactory(SessionFactory factory) {
		sessionFactory = factory;
	}
}
